package com.zygh.dlqwservice.module.service.impl;

import com.zygh.dlqwservice.common.constant.EmployeeResponseCodeConst;
import com.zygh.dlqwservice.common.en.EmployeeStatusEnum;
import com.zygh.dlqwservice.common.en.JudgeEnum;
import com.zygh.dlqwservice.common.pcdomain.ResponseDTO;
import com.zygh.dlqwservice.config.token.RequestTokenBO;
import com.zygh.dlqwservice.constant.CommonConst;
import com.zygh.dlqwservice.module.entity.Dept;
import com.zygh.dlqwservice.module.entity.SysUser;
import com.zygh.dlqwservice.module.entity.UserLoginLog;
import com.zygh.dlqwservice.module.entity.vo.LoginDetailVO;
import com.zygh.dlqwservice.module.entity.vo.SysUserLoginFormVO;
import com.zygh.dlqwservice.module.mapper.DeptMapper;
import com.zygh.dlqwservice.module.mapper.MeunMapper;
import com.zygh.dlqwservice.module.mapper.SysUserMapper;
import com.zygh.dlqwservice.module.service.LoginService;
import com.zygh.dlqwservice.util.DlqwDigestUtil;
import com.zygh.dlqwservice.util.SmartBeanUtil;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static java.time.LocalDateTime.now;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private MeunMapper meunMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private LoginTokenService loginTokenService;

    @Override
    public ResponseDTO<LoginDetailVO> login(@Valid SysUserLoginFormVO loginForm, HttpServletRequest request) {
        String loginPwd = DlqwDigestUtil.encryptPassword(CommonConst.Password.SALT_FORMAT, loginForm.getLoginPwd());
        SysUser sysUser = sysUserMapper.login(loginForm.getLoginName(), loginPwd);
        if (null == sysUser) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.LOGIN_FAILED);
        }
        if (EmployeeStatusEnum.DISABLED.equalsValue(sysUser.getIsDelete())) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.IS_DISABLED);
        }
        //jwt token赋值
        String compactJws = loginTokenService.generateToken(sysUser);
        LoginDetailVO loginDTO = SmartBeanUtil.copy(sysUser, LoginDetailVO.class);
        loginDTO.setMeunList(meunMapper.selectByUserId(sysUser.getUserId()));
        loginDTO.setXAccessToken(compactJws);
        Dept dept = deptMapper.selectById(sysUser.getDeptId());
        loginDTO.setDepartmentName(dept.getName());
        //登陆操作日志
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        UserLoginLog logEntity =
                UserLoginLog.builder()
                        .userId(sysUser.getUserId())
                        .userName(sysUser.getName())
                        .remoteIp(request.getRemoteAddr())
                        .remotePort(request.getRemotePort())
                        .remoteBrowser(userAgent.getBrowser().getName())
                        .remoteOs(userAgent.getOperatingSystem().getName())
                        .loginStatus(JudgeEnum.YES.getValue())
                        .createTime(now()).updateTime(now())
                        .build();
        logService.addLog(logEntity);

        return ResponseDTO.succData(loginDTO);
    }


}
